Skip to content

feat(vm): optimize V2 energy usage reset precision (TIP-945) - #7126

Merged
yanghang8612 merged 1 commit into
tronprotocol:release_v4.8.3from
yanghang8612:opt/energy-usage-reset-precision
Nov 25, 2026
Merged

yanghang8612 merged 1 commit into
tronprotocol:release_v4.8.3from
yanghang8612:opt/energy-usage-reset-precision

Conversation

@yanghang8612

@yanghang8612 yanghang8612 commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

Refine Stake 2.0 account energy usage reset precision under allowTvmResourceOptimization (TIP-945) and consolidate reset logic into EnergyProcessor:

  • ReceiptCapsule.java & VMActuator.java:
    • Record merged V2 window sizes (callerEnergyMergedWindowSizeV2 and originEnergyMergedWindowSizeV2, in ×1000 precision) alongside existing merged slot-window snapshots.
  • EnergyProcessor.java & TransactionTrace.java:
    • Move resetAccountUsage, resetAccountUsageV2, and checkResetEnergyUsage from TransactionTrace into EnergyProcessor.
    • In resetAccountUsageV2, when allowTvmResourceOptimization() is enabled, perform area calculation and window-merge selection uniformly in V2 (×1000) precision (currentSize2 / mergedSize2 / size2 / newSize2), using BigInteger when hardenCalculation() (ALLOW_HARDEN_RESOURCE_CALCULATION) is enabled and long V2 arithmetic otherwise.
    • Apply hardenCalculation() BigInteger arithmetic consistently to V1 resetAccountUsage under allowTvmResourceOptimization().
    • Tighten getMaxResetEnergyUsage() from 2 * totalEnergyCurrentLimit to 1 * totalEnergyCurrentLimit.
    • Preserve exact pre-proposal behavior when allowTvmResourceOptimization() is disabled.
  • Tip945VmTest.java:
    • Add and update unit tests covering V2 precision reset across allowTvmResourceOptimization × allowHardenResourceCalculation flag combinations, large-usage hardened reset, and the 1 * totalEnergyCurrentLimit upper-bound check.

Why are these changes required?

Under Stake 2.0 V2 window accounting, window sizes are stored at ×1000 precision, while resetAccountUsageV2 previously computed the post-execution area using truncated slot-level window sizes (getWindowSize). Aligning resetAccountUsageV2 to V2 precision under TIP-945, pairing it with hardenCalculation(), and tightening the reset upper bound to totalEnergyCurrentLimit keeps energy window accounting consistent and avoids intermediate precision loss or overflow. Moving the reset methods into EnergyProcessor also co-locates all energy usage calculation helpers in one processor class.

This PR has been tested by:

  • Unit Tests:
    • ./gradlew :framework:test --tests "org.tron.core.vm.nativecontract.Tip945VmTest" (16/16 passed).
    • ./gradlew :framework:test --tests "org.tron.core.db.TransactionTraceTest" (6/6 passed).
    • ./gradlew :framework:test --tests "org.tron.core.EnergyProcessorTest" (3/3 passed).
    • ./gradlew :framework:test --tests "org.tron.core.db.ResourceProcessorHardenTest" (10/10 passed).
  • Static Checks:
    • ./gradlew :framework:checkstyleMain :framework:checkstyleTest :plugins:checkstyleMain passed.
    • bash .github/scripts/check_math_usage.sh passed with 0 violations.
  • Environment: macOS x86_64, JDK 8.

Follow up

None.

Extra details

Strictly gated behind allowTvmResourceOptimization (TIP-945); pre-proposal behavior remains unchanged.

@yanghang8612 yanghang8612 added this to the GreatVoyage-v4.8.3 milestone Nov 24, 2026
@yanghang8612 yanghang8612 added the topic:vm VM, smart contract label Nov 24, 2026
return value * numerator / denominator;
}

public void resetAccountUsage(AccountCapsule accountCap,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unifying the resource-calculation methods in EnergyProcessor is the right split. With proposal 104 on, measuring and dividing the reservation on the same window fixes both the one-slot floor erasure and the equal-window scale-up. The old slot formula stays in place until 104 is enabled.

Move account energy usage reset methods from TransactionTrace to EnergyProcessor
and refine V2 window reset under allowTvmResourceOptimization (TIP-945):
- Record merged V2 window sizes (x1000 precision) for caller and origin in
  ReceiptCapsule and VMActuator.
- In EnergyProcessor.resetAccountUsageV2, perform V2 area and window selection
  using V2 window sizes when allowTvmResourceOptimization is enabled, using
  BigInteger when allowHardenResourceCalculation is active and long V2
  arithmetic otherwise, while preserving pre-proposal behavior when disabled.
- Apply allowHardenResourceCalculation BigInteger arithmetic to V1 reset as well
  under allowTvmResourceOptimization.
- Tighten getMaxResetEnergyUsage() from 2x to 1x totalEnergyCurrentLimit.
- Add and update unit tests in Tip945VmTest covering V2 precision reset across
  proposal and hardening flags, large-usage hardened reset, and the 1x cap.
@yanghang8612
yanghang8612 force-pushed the opt/energy-usage-reset-precision branch from 39f491a to 37a5c9b Compare November 25, 2026 06:11
@yanghang8612
yanghang8612 merged commit 3bddf57 into tronprotocol:release_v4.8.3 Nov 25, 2026
12 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:vm VM, smart contract

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants